package com.google.fbyoutube.client.widget;

import java.util.List;

import com.google.fbyoutube.client.page.Home;
import com.google.fbyoutube.shared.ytVideoEntry;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;

public class WidRelVIdeoList extends Composite {

    private VerticalPanel relVideoList;
    
    private int MAX_REL_VIDEO_NUM = 5;
    
	public WidRelVIdeoList(ytVideoEntry video) {
		

		CaptionPanel cptnpnlRelatedVideos = new CaptionPanel("Related Videos");
		initWidget(cptnpnlRelatedVideos);
		cptnpnlRelatedVideos.setSize("130px", "100%");
		
		relVideoList = new VerticalPanel();
		cptnpnlRelatedVideos.add(relVideoList);
		relVideoList.setWidth("100%");
		
		loadRelVideos(video);
	}
	
	private void loadRelVideos(final ytVideoEntry video) {
	  Home.videoService.getRelatedVideos(video.getId(), new AsyncCallback<List<ytVideoEntry>>() {
        
        @Override
        public void onSuccess(List<ytVideoEntry> result) {
          relVideoList.clear();
          int end = MAX_REL_VIDEO_NUM > result.size() ? result.size() : MAX_REL_VIDEO_NUM;
          for (int i = 0; i < end; ++i) {
            WidThumb widThumb = new WidThumb(result.get(i));
            relVideoList.add(widThumb);
          }
        }
        
        @Override
        public void onFailure(Throwable caught) {
          GWT.log("Failed to get related video for " + video.getId());
        }
      });
	  
	}

}
